﻿@page "/LinkIcon"
@using global::UniversityLink.DataModels
@using Newtonsoft.Json

<Flex Justify="center" Gap="10" Align="center">
    <Space>
        <Select DataSource="@(new[] { "全部", "类型", "名称" })"
                @bind-Value="@SelectValue"
                ItemValue="s => s"
                ItemLabel="s => s"
                Style="width: 10vw">
        </Select>
        <Search Placeholder="请输入搜索项" EnterButton="@("搜索")" @bind-Value="@SearchValue" Style="width: 30vw" Size="large"/>
    </Space>
    <Statistic GroupSeparator=" " Title="当前图标" Value="@IconModels.Length" TValue="int" Class="desktop-phone">
        <PrefixTemplate>
            <IconFont Type="icon-shuju"/>
        </PrefixTemplate>
    </Statistic>
    <Button Type="primary" OnClick="GetAll">
        恢复
    </Button>
</Flex>

<Divider Style="margin: 10px"/>

<GridRow Style="margin: 10px" Gutter="16">
    @foreach (var context in IconModels)
    {
        <GridCol Xs="12" Sm="12" Md="8" Lg="8" Xl="6" Xxl="6">
            <Card Hoverable Bordered="false" Style="border-radius: 10px;" BodyStyle="margin: 5px auto;text-align: center">
                <IconFont Type="@($"icon-{context.FontClass}")" Style="font-size: 30px"/>
                <br/>
                <p Style="font-size:18px;font-weight: bold">@context.Name</p>
                <Paragraph Copyable>@context.FontClass</Paragraph>
            </Card>
        </GridCol>
    }
</GridRow>

@code {

    private IconModel[] AllIcons { get; set; } = [];

    private IconModel[] IconModels { get; set; } = [];


    protected override async Task OnInitializedAsync()
    {
        var json = await File.ReadAllTextAsync("wwwroot/iconfont.json");
        AllIcons = JsonConvert.DeserializeObject<IconModel[]>(json) ?? [];
        IconModels = AllIcons;
    }

    private string _searchValue = "";
    private string _selectValue = "全部";

    private string SearchValue
    {
        get => _searchValue;
        set
        {
            _searchValue = value;
            Search();
        }
    }

    private string SelectValue
    {
        get => _selectValue;
        set
        {
            _selectValue = value;
            Search();
        }
    }

    private void GetAll()
    {
        SelectValue = "全部";
        SearchValue = "";
    }

    private void Search()
    {
        if (string.IsNullOrEmpty(SelectValue)) return;
        IconModels = SelectValue switch
        {
            "全部" => AllIcons,
            "类型" => AllIcons.Where(x => x.FontClass.Contains(SearchValue)).ToArray(),
            "名称" => AllIcons.Where(x => x.Name.Contains(SearchValue)).ToArray(),
            _ => IconModels
        };
    }

}